<template>
  <div class="all">
    <el-button type="primary" @click="handleAdd()" style="float: right">添加</el-button>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" ></el-table-column>
      <el-table-column label="名称" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.name" placeholder="请输入内容"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row)">修改</el-button>

        </template>
      </el-table-column>

      <el-table-column label="操作" >
        <template slot-scope="scope">

          <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog title="请输入类别名称" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="类别名称">
          <el-input v-model="form.name" placeholder="请输入类别名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import myaxios from "../config/myaxios";

export default {
  name: "TypePage",
  data() {
    return {
      tableData: [
        // {
        //   id: 1,
        //   name: '柜子'
        // }
      ],
      dialogVisible: false,
      form: {
        name: ''
      }
    }
  },
  created() {
    this.getall()
  },
  methods: {
    //
    getall() {
      myaxios.get('type')
        .then(res => {
          // console.log(res)
          if (res.data.code === 0) {
            this.tableData = res.data.data;
          } else {
            this.$message.error(res.data.message)
          }

        })
        .catch(error => {
          console.log(error);
          this.$message.error("系统繁忙，请重试")
        });
    },

    handleEdit(row) {
      console.log(row);
      myaxios.put("type", row).then(res => {
        // console.log(res)
        if (res.data.code === 0) {
          this.$message.success("修改成功")
          this.getall()
        } else {
          this.$message.error(res.data.message)
        }

      })
        .catch(error => {
          console.log(error);
          this.$message.error("系统繁忙，请重试")
        });
      // 在这里添加你的修改逻辑
    },
    handleDelete(id) {

      console.log(id);
      this.$alert('是否删除', '删除', {
        confirmButtonText: '确定',
        callback: action => {
          let urlSearchParams = new URLSearchParams();
          urlSearchParams.append("id", id)
          myaxios.delete("type", {
            params: urlSearchParams
          }).then(res => {
            // console.log(res)
            if (res.data.code === 0) {
              this.$message.success("删除成功")
              this.getall()
            } else {
              this.$message.error(res.data.message)
            }

          })
            .catch(error => {
              console.log(error);
              this.$message.error("系统繁忙，请重试")
            });
        }
      });


      // 在这里添加你的删除逻辑
    },
    handleAdd() {
      this.dialogVisible = true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          myaxios.post('type', this.form)
            .then(response => {
              console.log(response);
              if (response.data.code === 0) {
                this.$message.success("添加成功")
                this.getall()
              } else {
                this.$message.error(response.data.message)
              }
              this.dialogVisible = false;
              this.form.name = null
            })
            .catch(error => {
              console.log(error);
            });
        } else {
          console.log('请填写完整');
          return false;
        }
      });
    }

  }
}
</script>

<style scoped>

</style>
